<template>
  <div class="code-type">
    <el-card class="status-card" shadow="hover">
      <div class="status-content">
        <!-- 访客信息 -->
        <div class="visitor-info">
          <el-descriptions :column="mobile ? 1 : 2" border>
            <el-descriptions-item label="访客人员">
              {{ formData.XM || "--" }} <el-button type="primary" text @click="handleReset">
            <el-icon>
              <Refresh />
            </el-icon>
            重新填写
          </el-button>
            </el-descriptions-item>
            <el-descriptions-item label="公司名称">
              {{ formData.LFRGSMC || "--" }}
            </el-descriptions-item>
            <el-descriptions-item label="联系电话">
              {{ formData.LXDH || "--" }}
            </el-descriptions-item>
            <el-descriptions-item label="来访人数">
              {{ formData.RS || "--" }}
            </el-descriptions-item>
            <el-descriptions-item label="进场事由">
              {{ formData.JCSY || "--" }}
            </el-descriptions-item>
            <el-descriptions-item label="通行码状态">
              <el-tag :type="getStatusTagType(formData.SHZT)">
                {{ getStatusText(formData.SHZT) }}
              </el-tag>
            </el-descriptions-item>
           <!--  <el-descriptions-item label="有效期">
              {{ getValidityPeriod() }}
            </el-descriptions-item> -->
            <el-descriptions-item v-if="formData.SHZT === '3'" label="驳回原因">
              <span class="reject-reason">{{ formData.BHYY || "--" }}</span>
            </el-descriptions-item>
          </el-descriptions>
        </div>
        <!-- 通行码显示 -->
        <div class="title">通行证有效期：{{ formData.SHZT =='2' ? formData.YXQKSSJ + '~' + formData.YXQJSSJ : '--' }}</div>
        <div class="code-display">
          
          <!--   <div class="code-text">
              {{ getCodeText(formData.SHZT) }}
            </div>
            <div class="code-label">
              {{ getCodeLabel(formData.SHZT) }}
            </div> -->
          <img :src="getCodeImage(formData.SHZT)" alt="">
        </div>
        <div class="shuaxin" @click="replace"> <el-icon>
            <Refresh />
          </el-icon>点击刷新通行证</div>
        <!-- 状态说明 -->
        <div class="status-explanation">
          <el-alert :title="getStatusExplanation(formData.SHZT)" :type="getAlertType(formData.SHZT)" :closable="false"
            show-icon />
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { Refresh } from "@element-plus/icons-vue";
import type { VisitorInfo } from "~/types/api";
import { toRefs, computed } from "vue";

interface Props {
  formData: Partial<VisitorInfo>;
}

interface Emits {
  (e: "getDetail"): void;
  (e: "reset"): void;
}

const props = withDefaults(defineProps<Props>(), {
  formData: () => ({} as Partial<VisitorInfo>)
});
const { formData } = toRefs(props);
const mobile = computed(() => process.client ? window.innerWidth <= 768 : false);
const emit = defineEmits<Emits>();

// 获取配置
const config = useRuntimeConfig()
const imageUrl = `${config.public.apiBase}/ezsafe-server/static/miniapp/image`

// 状态映射
const statusMap = {
  "0": {
    // 待审核
    codeClass: "code-orange",
    codeText: "审",
    codeLabel: "审核中",
    statusImage: `${imageUrl}/statust_0.png`,
    tagType: "warning",
    statusText: "待审核",
    explanation: "您的申请正在审核中，请耐心等待",
    alertType: "warning",
  },
  "2": {
    // 审核通过
    codeClass: "code-green",
    codeText: "通",
    codeLabel: "通行",
    statusImage: `${imageUrl}/statust_2.png`,
    tagType: "success",
    statusText: "审核通过",
    explanation: "审核已通过，您可以在有效期内通行",
    alertType: "success",
  },
  "1": {
    // 审核驳回
    codeClass: "code-red",
    codeText: "拒",
    codeLabel: "已驳回",
    statusImage: `${imageUrl}/statust_1.png`,
    tagType: "danger",
    statusText: "审核驳回",
    explanation: "审核未通过，请根据驳回原因重新填写信息",
    alertType: "error",
  },
};

const getCodeClass = (status?: string) => {
  return statusMap[status as keyof typeof statusMap]?.codeClass || "code-gray";
};

const getCodeText = (status?: string) => {
  return statusMap[status as keyof typeof statusMap]?.codeText || "--";
};
const getCodeImage = (status?: string) => {
  return statusMap[status as keyof typeof statusMap]?.statusImage || "--";
};
const getCodeLabel = (status?: string) => {
  return statusMap[status as keyof typeof statusMap]?.codeLabel || "未知";
};

const getStatusTagType = (status?: string) => {
  return statusMap[status as keyof typeof statusMap]?.tagType || "info";
};

const getStatusText = (status?: string) => {
  return statusMap[status as keyof typeof statusMap]?.statusText || "未知状态";
};

const getStatusExplanation = (status?: string) => {
  return (
    statusMap[status as keyof typeof statusMap]?.explanation || "状态信息未知"
  );
};

const getAlertType = (status?: string) => {
  return statusMap[status as keyof typeof statusMap]?.alertType || "info";
};

const getValidityPeriod = () => {
  if (
    props.formData.SHZT === "2" &&
    props.formData.YXQKSSJ &&
    props.formData.YXQJSSJ
  ) {
    return `${props.formData.YXQKSSJ} 至 ${props.formData.YXQJSSJ}`;
  }
  return "--";
};
const replace = () => {
  emit('getDetail')
}
const handleReset = () => {
  emit("reset");
};
</script>

<style scoped>
.code-type {
  padding: 20px 0;
}

.status-card {
  margin-bottom: 2rem;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-title {
  font-size: 1.25rem;
  font-weight: 600;
  color: #303133;
}

.status-content {
  text-align: center;
}

.code-display {
  height: 250px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 2rem;
}

.code-display img {
  width: 220px;
  height: 220px;
  object-fit: contain;
  border-radius: 8px;
}

.shuaxin {
  display: flex;
  align-items: center;
  justify-content: center;
  color: #bbb;
  font-size: 14px;
  margin: 20px 0px 10px;
}

.code-circle {
  width: 200px;
  height: 200px;
  border-radius: 50%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  margin: 0 auto;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}
.title {
    text-align: center;
    margin: 10px 0 15px;
    font-weight: 800;
  }
.code-green {
  background: linear-gradient(135deg, #67c23a, #85ce61);
  color: white;
}

.code-orange {
  background: linear-gradient(135deg, #e6a23c, #ebb563);
  color: white;
}

.code-red {
  background: linear-gradient(135deg, #f56c6c, #f78989);
  color: white;
}

.code-gray {
  background: linear-gradient(135deg, #909399, #a6a9ad);
  color: white;
}

.code-text {
  font-size: 4rem;
  font-weight: bold;
  line-height: 1;
  margin-bottom: 0.5rem;
}

.code-label {
  font-size: 1.2rem;
  font-weight: 500;
}

.visitor-info {
  margin-bottom: 2rem;
}

.reject-reason {
  color: #f56c6c;
  font-weight: 500;
}

.status-explanation {
  margin-top: 1rem;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .code-type {
    padding: 15px 0;
  }

  .card-header {
    flex-direction: column;
    gap: 1rem;
    align-items: flex-start;
  }

  .code-display {
    height: 200px;
  }

  .code-display img {
    width: 150px;
    height: 150px;
  }

  .code-circle {
    width: 150px;
    height: 150px;
  }

  .code-text {
    font-size: 3rem;
  }
}

@media (max-width: 480px) {
  .code-display {
    height: 180px;
  }

  .code-display img {
    width: 120px;
    height: 120px;
  }
}
</style>
